package org.qiyu.live.web.starter.error;

import jakarta.servlet.http.HttpServletRequest;
import org.dubbo.live.common.interfaces.vo.WebResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 类功能描述: 全局异常拦截
 *
 * @Author: 张广焱
 * @Date: 2024-06-23  13:12:17
 * @Version: 1.0.0
 */
@ControllerAdvice   //对controller 层所有请求进行拦截
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class) //指定监听的异常类型
    @ResponseBody //捕获所有异常
    public WebResponseVO errorHandler(HttpServletRequest request, Exception e) {
        LOGGER.error(request.getRequestURI() + ",error is ", e);
        return WebResponseVO.sysError("系统异常");
    }


    /**
     * @Description: 异常捕获监听自定义类
     * @Author: 张广焱
     * @Date: 2024-06-23  13:14:41
     */
    @ExceptionHandler(value = QiyuErrorException.class)
    @ResponseBody
    public WebResponseVO sysErrorHandler(HttpServletRequest request, QiyuErrorException e) {
        //业务异常，参数传递有误,都会走到这里
        LOGGER.error(request.getRequestURI() + ",error code is {},error msg is {}", e.getErrorCode(), e.getErrorMsg());
        return WebResponseVO.bizError(e.getErrorCode(), e.getErrorMsg());
    }
}
